package com.ihelp.proxy;

import com.alibaba.fastjson2.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class LogProxy {
    private static final String aopControllerLogPageName = "execution(* com.ihelp.controller.*.*(..))";
    private static final String aopServiceLogPageName = "execution(* com.ihelp.service.*.*(..))";


    //@Before注解表示作为前置通知
    @Order(value = 1)
    @Before(value = aopControllerLogPageName)
    public void before(JoinPoint jp) {
//        String requestParametersJSON = JSONArray.toJSONString(jp.getArgs());
        log.info("类:" + jp.getSignature().getDeclaringTypeName() + ",方法名:" + jp.getSignature().getName() + ",参数:" + jp.getArgs());
    }

    //出现异常 AfterReturn失效
    @AfterReturning(value = aopControllerLogPageName, returning = "returnValue")
    public void afterReturning(JoinPoint jp, Object returnValue) {
//        String responseParametersJSON = JSONArray.toJSONString(returnValue);
        log.info("类:" + jp.getSignature().getDeclaringTypeName() + ",方法名:" + jp.getSignature().getName() + ",返回值:" + returnValue);
    }

    @After(value = aopControllerLogPageName)
    public void after() {

    }

    //Controller错误日志
    @AfterThrowing(value = aopControllerLogPageName, throwing = "error")
    public void afterThrowing(JoinPoint jp, Throwable error) {
        log.error("类:" + jp.getSignature().getDeclaringTypeName() + ",方法名:" + jp.getSignature().getName() + ",异常:" + error);
    }

    //Service错误日志
    @AfterThrowing(value = aopServiceLogPageName, throwing = "error2")
    public void afterThrowing2(JoinPoint jp, Throwable error2) {
        log.error("类:" + jp.getSignature().getDeclaringTypeName() + ",方法名:" + jp.getSignature().getName() + ",异常:" + error2);
    }


    @Pointcut(value = aopControllerLogPageName)
    public void aopControllerLogPoint() {

    }

    @Around("aopControllerLogPoint()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("--------------------------------------------分隔符---------------------------------------------------------");
        return proceedingJoinPoint.proceed();
    }


}
